<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Ad_Models_Mysql_Zone extends Modules_Ad_Models_Zone
{
	protected $_table = 'ad_zone';
	
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'ad_zone 
							WHERE zone_id = ' . $this->_conn->escape($id) . ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function getByPageName($pageName)
	{
		$sql = 'SELECT z.* FROM ' . $this->_conn->_tablePrefix . 'ad_zone AS z 
				INNER JOIN ' . $this->_conn->_tablePrefix . 'ad_banner_page_assoc AS bps
					ON z.zone_id = bps.zone_id
				INNER JOIN ' . $this->_conn->_tablePrefix . 'core_page AS p
					ON p.page_id = bps.page_id
				WHERE p.name = "' . $this->_conn->escape($pageName) . '"
				GROUP BY z.zone_id  
				ORDER BY z.created_date DESC';
		$rs = $this->_conn->query($sql);
		$zones = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$zones[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $zones;
	}
	
	public function getAllZones($active = false)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS z';
		if ($active) {
			$sql .= ' WHERE z.status = "active"';
		}
		$sql .= ' ORDER BY z.created_date DESC';
		$rs = $this->_conn->query($sql);
		$zones = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$zones[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $zones;
	}
	
	public function add($zone)
	{
		$zoneId = $this->_conn->insert($zone, $this->_conn->_tablePrefix . $this->_table);
		return $zoneId;	
	}
	
	public function update($zone)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET code = "%s", description = "%s", status = "%s",
						width = "%s", height = "%s", price = "%s"
					WHERE zone_id = "%d"';
		$sql = sprintf($sql, $this->_conn->escape($zone['code']),
							$this->_conn->escape($zone['description']),
							$this->_conn->escape($zone['status']),
							$this->_conn->escape($zone['width']),
							$this->_conn->escape($zone['height']),
							$this->_conn->escape($zone['price']),	
							$this->_conn->escape($zone['zone_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
	
	public function delete($zoneId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'ad_zone 
							WHERE zone_id = ' . $this->_conn->escape($zoneId);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function find($condition = array(), $offset = null, $limit = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'ad_zone AS z ORDER BY z.created_date DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$zones = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$zones[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $zones;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) as num_zones FROM ' . $this->_conn->_tablePrefix . 'ad_zone';
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_zones'];
	}
	
	public function updateStatus($zoneId, $status)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . 'ad_zone 
					SET status = "%s" 
					WHERE zone_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($status),
							$this->_conn->escape($zoneId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
}